﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using jaysJourney_v01.States;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;

namespace jaysJourney_v01.Common
{
    public class StateManager
    {
        /**
         * MEMBER VARIABLES
         * */

        // From Parent

        private JaysJourneyMain game;
        private SpriteBatch mainSpriteBatch;

        // Common

        private String activeState;
        private String lastState;
        private List<State> statesList;

        /**
         * METHODS
         * */

        // Constructor

        public StateManager(JaysJourneyMain game) 
        {
            this.game = game;
            this.mainSpriteBatch = this.game.mainSpriteBatch;
            statesList = new List<State>();
            initializeList(); //alle states in dieser methode erstellen
            activeState = "loginState"; // startState
            getState(activeState).onEnterState();
        }

        // MAIN UPDATE

        public void update(GameTime gameTime)
        {
            foreach (State state in statesList)
            {
                if (state.getName() == activeState)
                {
                    state.stateUpdate(gameTime);
                }
            }
        }

        // MAIN DRAW

        public void draw(GameTime gameTime)
        {
            foreach (State state in statesList)
            {
                if (state.getName() == activeState)
                {
                    state.stateDraw(gameTime);
                }
            }
        }

        // set active state

        public void setActiveState(String activeState)
        {
            // set new activeState

            this.lastState = this.activeState;
            this.activeState = activeState;

            // trigger single event on activation
            foreach (State state in statesList)
            {
                if (state.getName() == activeState)
                {
                    state.started();
                }
            }
        }

        // add new state

        public void addState(State state)
        {
            statesList.Add(state);
            state.initialize();
        }

        // init list

        private void initializeList()
        {
            addState(new LoginState("loginState", this));
            addState(new RegisterState("registerState", this));
            addState(new MenuState("menuState", this));
            addState(new LoadWorldState("loadWorldState", this));
            addState(new SettingsState("settingsState", this));
            addState(new TutorialState("tutorialState", this));
            addState(new PlayState("playState", this));
            addState(new EditorState("editorState", this));
            addState(new PauseState("pauseState", this));
            addState(new DeathState("deathState", this));
            addState(new WinState("winState", this));
            addState(new CurWorldDetailsState("curWorldDetailState", this));
        }

        // getGame

        public JaysJourneyMain getGame()
        {
            return this.game;
        }

        // getlast

        public String getLastActiveState()
        {
            return this.lastState;
        }

        public String getActiveState()
        {
            return this.activeState;
        }
        // get state

        public State getState(String name)
        {
            foreach (State state in statesList)
            {
                if (state.getName() == name) return state;
            }
            return null;
        }

        // getSpriteBatch

        public SpriteBatch getMainSB()
        {
            return this.mainSpriteBatch;
        }
    }
}
